import 'page_request.dart';
import 'sort.dart';

/// 分页结果接口
abstract class BasePageResult<T> {
  /// 当前页内容
  List<T> get content;

  /// 分页参数
  BasePageRequest get pageReq;

  /// 当前页元素数量
  int get numberOfElements => content.length;

  /// 页码
  int get number => pageReq.pageNumber;

  /// 页大小
  int get size => pageReq.pageSize;

  /// 是否有内容
  bool get hasContent => content.isNotEmpty;

  /// 是否为空
  bool get isEmpty => content.isEmpty;

  /// 是否为第一页
  bool get isFirst => number == 1;

  /// 是否为最后一页
  bool get isLast;

  /// 是否有下一页
  bool get hasNext => !isLast;

  /// 是否有上一页
  bool get hasPrevious => number > 1;

  /// 排序信息
  Sort get sort => pageReq.sort;

  /// 获取下一页分页参数
  BasePageRequest? get nextpageReq;

  /// 获取上一页分页参数
  BasePageRequest? get previouspageReq;

  /// 转换页面内容
  BasePageResult<U> map<U>(U Function(T) converter);
}

/// 分页结果实现
class PageResult<T> implements BasePageResult<T> {
  @override
  final List<T> content;

  @override
  final BasePageRequest pageReq;

  /// 总元素数量
  final int totalElements;

  const PageResult({
    required this.content,
    required this.pageReq,
    required this.totalElements,
  });

  /// 创建空分页结果
  factory PageResult.empty([BasePageRequest? pageReq]) {
    return PageResult(
      content: const [],
      pageReq: pageReq ?? PageRequest.first(20),
      totalElements: 0,
    );
  }

  /// 从单页数据创建分页结果
  factory PageResult.single(List<T> content, [BasePageRequest? pageReq]) {
    return PageResult(
      content: content,
      pageReq: pageReq ?? PageRequest.first(content.length),
      totalElements: content.length,
    );
  }

  /// 总页数
  int get totalPages {
    if (pageReq.pageSize == 0) return 1;
    return (totalElements / pageReq.pageSize).ceil();
  }

  @override
  bool get isLast => number >= totalPages;

  /// 是否有内容
  @override
  bool get hasContent => content.isNotEmpty;

  /// 是否为空
  @override
  bool get isEmpty => content.isEmpty;

  /// 是否为第一页
  @override
  bool get isFirst => number == 1;

  /// 是否有下一页
  @override
  bool get hasNext => !isLast;

  /// 是否有上一页
  @override
  bool get hasPrevious => number > 1;

  /// 当前页元素数量
  @override
  int get numberOfElements => content.length;

  /// 页码
  @override
  int get number => pageReq.pageNumber;

  /// 页大小
  @override
  int get size => pageReq.pageSize;

  /// 排序信息
  @override
  Sort get sort => pageReq.sort;

  @override
  BasePageRequest? get nextpageReq {
    return hasNext ? pageReq.next : null;
  }

  @override
  BasePageRequest? get previouspageReq {
    return hasPrevious ? pageReq.previous : null;
  }

  /// 获取指定页码的分页参数
  BasePageRequest? getpageReq(int pageNumber) {
    if (pageNumber < 1 || pageNumber > totalPages) {
      return null;
    }

    if (pageReq is PageRequest) {
      return (pageReq as PageRequest).withPage(pageNumber);
    }

    return PageRequest(pageNumber, pageReq.pageSize, pageReq.sort);
  }

  @override
  PageResult<U> map<U>(U Function(T) converter) {
    final mappedContent = content.map(converter).toList();
    return PageResult<U>(
      content: mappedContent,
      pageReq: pageReq,
      totalElements: totalElements,
    );
  }

  /// 转换为另一种类型的分页结果
  PageResult<U> mapTo<U>(List<U> newContent) {
    return PageResult<U>(
      content: newContent,
      pageReq: pageReq,
      totalElements: totalElements,
    );
  }

  /// 获取分页统计信息
  PageInfo get pageInfo {
    return PageInfo(
      pageNumber: number,
      pageSize: size,
      totalElements: totalElements,
      totalPages: totalPages,
      numberOfElements: numberOfElements,
      isFirst: isFirst,
      isLast: isLast,
      hasNext: hasNext,
      hasPrevious: hasPrevious,
    );
  }

  @override
  String toString() {
    return 'PageResult{number: $number, size: $size, totalElements: $totalElements, '
        'totalPages: $totalPages, numberOfElements: $numberOfElements}';
  }

  @override
  bool operator ==(Object other) =>
      identical(this, other) ||
      other is PageResult &&
          runtimeType == other.runtimeType &&
          _listEquals(content, other.content) &&
          pageReq == other.pageReq &&
          totalElements == other.totalElements;

  @override
  int get hashCode =>
      content.hashCode ^ pageReq.hashCode ^ totalElements.hashCode;

  bool _listEquals<T>(List<T> a, List<T> b) {
    if (a.length != b.length) return false;
    for (int i = 0; i < a.length; i++) {
      if (a[i] != b[i]) return false;
    }
    return true;
  }
}

/// 分页信息
class PageInfo {
  /// 页码
  final int pageNumber;

  /// 页大小
  final int pageSize;

  /// 总元素数量
  final int totalElements;

  /// 总页数
  final int totalPages;

  /// 当前页元素数量
  final int numberOfElements;

  /// 是否为第一页
  final bool isFirst;

  /// 是否为最后一页
  final bool isLast;

  /// 是否有下一页
  final bool hasNext;

  /// 是否有上一页
  final bool hasPrevious;

  const PageInfo({
    required this.pageNumber,
    required this.pageSize,
    required this.totalElements,
    required this.totalPages,
    required this.numberOfElements,
    required this.isFirst,
    required this.isLast,
    required this.hasNext,
    required this.hasPrevious,
  });

  /// 转换为Map
  Map<String, dynamic> toMap() {
    return {
      'pageNumber': pageNumber,
      'pageSize': pageSize,
      'totalElements': totalElements,
      'totalPages': totalPages,
      'numberOfElements': numberOfElements,
      'isFirst': isFirst,
      'isLast': isLast,
      'hasNext': hasNext,
      'hasPrevious': hasPrevious,
    };
  }

  @override
  String toString() {
    return 'PageInfo{pageNumber: $pageNumber, pageSize: $pageSize, '
        'totalElements: $totalElements, totalPages: $totalPages, '
        'numberOfElements: $numberOfElements}';
  }
}
